<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Good extends Model
{
    use HasFactory;

//    可以批量赋值的字段
    protected $fillable = ['user_id','category_id','description','price','stock','cover','pics','details',];

    /**
     * 强制转换的属性
     *
     * @var array
     */
    protected $casts = [
        'pics' => 'array',
    ];

//    追加额外的属性
    protected  $appends = ['cover_url'];

    public function getCoverUrlAttribute()
    {
        return $this->cover;
    }

    /**
     * 商品所属分类
     * 模型关联
     */
    public function category()
    {
        return $this->belongsTo(Category::class,'category_id','id');
    }

    /**
     * 商品所属用户
     * 模型关联
     */
    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }

    /**
     * 商品所属用户
     * 模型关联
     */
    public function comments()
    {
        return $this->hasMany(Comment::class,'goods_id','id');
    }
}
